package org.zs.miracle.service.comment.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.zs.miracle.common.base.dto.BaseLongIdDTO;
import org.zs.miracle.common.base.dto.BaseManyLongIdDTO;
import org.zs.miracle.common.util.ModelMapperUtil;
import org.zs.miracle.entity.comment.CommentEntity;
import org.zs.miracle.mapper.CommentMapper;
import org.zs.miracle.service.comment.ICommentService;
import org.zs.miracle.service.comment.model.dto.AddCommentDTO;
import org.zs.miracle.service.comment.model.dto.CommentQueryPageDTO;
import org.zs.miracle.service.comment.model.dto.EditCommentDTO;
import org.zs.miracle.service.comment.model.vo.CommentVO;

/**
 * 用户评论表Service服务接口实现层
 */
@Service
@AllArgsConstructor
public class ICommentServiceImpl implements ICommentService {
  
  @Resource
  CommentMapper commentMapper;
  
  @Override
  public Page<CommentVO> pageQueryList(CommentQueryPageDTO inputDTO) {
    
    // 构建查询条件
    LambdaQueryWrapper<CommentEntity> queryWrapper = Wrappers.lambdaQuery();
    
    // 执行查询用户
    Page<CommentVO> page =
        commentMapper.selectPage(inputDTO.toMybatisPageObject(), queryWrapper, CommentVO.class);
    
    return page;
  }
  
  // @Transactional
  @Override public List<CommentVO> getList(BaseLongIdDTO inputDTO) {
    LambdaQueryWrapper<CommentEntity> queryWrapper = Wrappers.lambdaQuery();
    queryWrapper.eq(CommentEntity::getPostId, inputDTO.getId());
    return commentMapper.selectList(queryWrapper, CommentVO.class);
  }
  
  @Override
  public Boolean delComment(BaseManyLongIdDTO inputDTO) {
    
    // 删除用户
    commentMapper.deleteByIds(inputDTO.getIds());
    return true;
  }
  
  @Override
  public Boolean addComment(AddCommentDTO inputDTO) {
    
    // 将DTO转换为实体对象
    CommentEntity addEntity = ModelMapperUtil.map(inputDTO, CommentEntity.class);
    // 插入数据库
    commentMapper.insert(addEntity);
    
    return true;
  }
  
  @Override
  public Boolean editComment(EditCommentDTO inputDTO) {
    
    // 更新的数据
    CommentEntity updateEntity = ModelMapperUtil.map(inputDTO, CommentEntity.class);
    
    // 执行更新
    commentMapper.updateById(updateEntity);
    
    return true;
  }
}